package com.documents.common.core.aop;

import java.lang.reflect.Method;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.aspectj.lang.ProceedingJoinPoint;


import com.opensymphony.xwork2.ActionContext;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
public class LogAspect {

//    @Resource
//    private SystemLogService systemLogService;
    private Log logger = LogFactory.getLog(LogAspect.class);
       

	public Object doSystemLog(ProceedingJoinPoint point) throws Throwable {

        String methodName = point.getSignature().getName();

        // 目标方法不为空
        if (StringUtils.isNotEmpty(methodName)) {
            // set与get方法除外
            if (!(methodName.startsWith("set") || methodName.startsWith("get"))) {

                Class targetClass = point.getTarget().getClass();
                Method method = targetClass.getMethod(methodName);

                if (method != null) {

                    boolean hasAnnotation = method.isAnnotationPresent(Action.class);

                    if (hasAnnotation) {
                        Action annotation = method.getAnnotation(Action.class);

                        String methodDescp = annotation.description();
                        if (logger.isDebugEnabled()) {
                            logger.debug("Action method:" + method.getName()
                                    + " Description:" + methodDescp);
                        }
                        // 取到当前的操作用户
                        Map session = ActionContext.getContext().getSession();
//                        Users appUser = (Users) session.get("user");
//                        String currentrole=(String) session.get("currentrole");
//                        if (appUser != null&&currentrole.equals(UserTypeUtil.CH_SYSTEM)) {
//                     
//                            
//                            try {
//                                SystemLog sysLog = new SystemLog();
//                                Date date = new Date();
//                                Timestamp ts = new Timestamp(date.getTime());
//                                sysLog.setCreateDate(ts);
//                                sysLog.setUserId(appUser.getId());
//                                sysLog.setUserName(appUser.getTruename());
//                                sysLog.setOperation(methodDescp);
//                                sysLog.setLoginIp(getLocalIP());
//                                systemLogService.save(sysLog);
//                            } catch (Exception ex) {
//                                logger.error(ex.getMessage());
//                            }
//                        }

                    }
                }

            }
        }
        return point.proceed();
    }
        
        	/**
	 * 如果存在代理的时候也可以获取客户端的真实ip地址
	 * @return
	 */
	public static String getLocalIP() {
		HttpServletRequest request = ServletActionContext.getRequest();
		String ip = request.getHeader("x-forwarded-for");
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}
		return ip;
	}
}